package cn.aloxc.tianya.reader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import cn.aloxc.tools.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Activity_test_xmlPull extends Activity
{
	OnItemClickListener listener = null;
	Button call = null;
	OnClickListener callListener = null;
	ArrayList<Map<String,String>> list = null;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Bundle data = this.getIntent().getExtras();
		setTitle(data.getString("testItem"));
		try
		{
			this.getData();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
		}
		ListView lv = new ListView(this);
		SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.activity_test_list_rss_xmlpull,
        		new String[]{RSSItem.TITLE,RSSItem.PUBDATE},
        		new int[]{R.id.test_xmllist_content_title_forXmlPull,R.id.test_xmllist_content_pubdate_forXmlPull}
        		);
        lv.setAdapter(adapter);
        setContentView(lv);
        listener = new OnItemClickListener(){
			@SuppressWarnings("unchecked")
			public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
				//setTitle(parent.getItemAtPosition(pos).toString());
				Map<String, String> item = (Map<String, String>)parent.getItemAtPosition(pos);
				Bundle b = new Bundle();
				for (Iterator it = item.entrySet().iterator(); it.hasNext();) {
				      Map.Entry e = (Map.Entry) it.next();
				      b.putString(e.getKey().toString(),e.getValue().toString());				      
				}
				Intent intent = new Intent(Activity_test_xmlPull.this,Activity_test_xmllist_content.class);
				intent.putExtra("android.intent.extra.rssItem", b);
				startActivity(intent);
			}};
		lv.setOnItemClickListener(listener);
		callListener = new Button.OnClickListener(){
			public void onClick(View btn) {
			}};

	}
	public final String RSS_URL = "http://blog.s135.com/feed.php";
	private void getData() throws IOException, XmlPullParserException{
		URL url = new URL(RSS_URL);
		InputStream inputStream = url.openStream();
		XmlPullParser xmlpull = Xml.newPullParser();
		xmlpull.setInput(inputStream, "utf-8");
		int eventCode = xmlpull.getEventType();
		Map<String,String> map = null;
		while(eventCode != XmlPullParser.END_DOCUMENT){
			switch (eventCode)
			{
			case XmlPullParser.START_DOCUMENT:{
				list = new ArrayList<Map<String, String>>();
				break;
			}
			case XmlPullParser.START_TAG:{
				if("item".equalsIgnoreCase(xmlpull.getName())){
					map = new HashMap<String,String>();
				}else if(map != null){
					if("title".equalsIgnoreCase(xmlpull.getName())){
						map.put(RSSItem.TITLE,xmlpull.nextText());
					}else if("link".equalsIgnoreCase(xmlpull.getName())){
						map.put(RSSItem.LINK,xmlpull.nextText());
					}else if("pubdate".equalsIgnoreCase(xmlpull.getName())){
						map.put(RSSItem.PUBDATE,xmlpull.nextText());
					}else if("description".equalsIgnoreCase(xmlpull.getName())){
						map.put(RSSItem.DESCRIPTION,xmlpull.nextText());
					}else if("category".equalsIgnoreCase(xmlpull.getName())){
						map.put(RSSItem.CATEGORY,xmlpull.nextText());
					}else if("guid".equalsIgnoreCase(xmlpull.getName())){
						map.put(RSSItem.GUID,xmlpull.nextText());
					}
				}
				break;
			}
			case XmlPullParser.END_TAG:{
				if("item".equalsIgnoreCase(xmlpull.getName()) && map != null){
					list.add(map);
					map = null;
				}
				break;
			}
			default:
				break;
			}
			eventCode = xmlpull.next();
		}
		Log.i("list.size()", String.valueOf(list.size()));
	}
}
